package info.douyi.dps.serviceImpl;

import info.douyi.dps.mapper.BeanMapper;
import info.douyi.dps.model.Bean;
import info.douyi.dps.service.BeansService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;


@Service
public class BeansServiceImpl implements BeansService {
    private static final Logger logger = LoggerFactory.getLogger(BeansServiceImpl.class);

    @Autowired
    private BeanMapper beanMapper;

    @Override
    public List<Bean> getBeans() {
        try {
            if (beanMapper == null) {
                logger.error("BeanMapper is null, unable to perform query.");
                return Collections.emptyList();
            }
            List<Bean> beans = beanMapper.selectByExample(null);
            return beans != null ? beans : Collections.emptyList();
        } catch (Exception e) {
            logger.error("An error occurred while querying beans.", e);
            return Collections.emptyList();
        }
    }

    @Override
    public void insertBean(Bean bean) {
        try{
            if(beanMapper == null) {
                logger.error("BeanMapper is null, unable to perform query.");
                return ;
            }
            beanMapper.insertSelective(bean);
        } catch (Exception e) {
            logger.error("An error occurred while inserting bean.", e);
            return;
        }
    }


}    